package _01_第一季._00_公共接口;
/*
    线性表的抽象公共类
 */
public abstract class AbstractList<E> implements List<E> {
    protected int size;

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(E element) {
        return indexOf(element) != ELEMENT_NOT_FOUND;
    }

    @Override
    public void add(E element) {
        add(size,element);
    }

    protected void outOfBounds(int index){
        throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size);
    }

    protected void rangeCheck(int index){
        if(index<0 || index>=size){
            outOfBounds(index);
        }
    }

    protected void rangeCheckForAdd(int index){
        if(index<0 || index > size){
            outOfBounds(index);
        }
    }
}
